﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class EnemyBaseFixed : EnemyBase
{

    [Header("基础配置")]

    [Header("死亡效果")]
    [Tooltip("死亡动作")]
    protected SeqAction dieAction = new() { intervalTime = 1f / 60f };

    [Header("射击配置")]
    [Tooltip("子弹预制件")]
    public GameObject bulletPrefab;
    [Tooltip("射击动作")]
    public SeqAction fireAction = new() { beginWaitTime = 2f, intervalTime = 0.5f };
    [Tooltip("弹匣大小")]
    public int boxMagazineSize = 3;


    [Tooltip("面朝方向")]
    protected Vector2 lookAt;
    [Tooltip("状态机")]
    private readonly StateMachine<EnumState> stateMachine = new();
    [Tooltip("是否需要转身")]
    protected bool needTurnAround = true;
    [Tooltip("被命中时的音效")]


    protected Vector2[] dieMovePixel = new Vector2[]
{
            new Vector2(0,0),
            new Vector2(0,4),
            new Vector2(0,7),
            new Vector2(1,10),
            new Vector2(1,13),
            new Vector2(1,15),
            new Vector2(2,18),
            new Vector2(2,20),
            new Vector2(3,22),
            new Vector2(3,24),
            new Vector2(3,25),
            new Vector2(4,27),
            new Vector2(4,29),
            new Vector2(5,30),
            new Vector2(5,30),
            new Vector2(6,31),
};
    protected override void Init()
    {
        base.Init();
        lookAt = Vector2.right;
        boomPrefab=Constant.Prefabs.EnemyBoom1.Load();

        //初始化状态机
        stateMachine.Registe(this);
        state = EnumState.None;

    }

    protected virtual void DefaultUpdateAction(bool stateChange, EnumState inState, out EnumState outState)
    {
        outState = inState;
    }
    protected virtual void PreUpdateAction(EnumState inState)
    {
    }
    protected virtual void PostUpdateAction(bool stateChange, EnumState inState, EnumState outState)
    {
    }

    protected virtual void CommonFixedUpdate()
    {
        if (running)
        {
            stateMachine.Invoke(state, out state, DefaultUpdateAction, PreUpdateAction, PostUpdateAction);

            UpdateAppear();
            //是否转身
            if (needTurnAround)
            {
                transform.localScale = new Vector3(lookAt.x == 0 ? 1 : lookAt.x, 1, 1);
            }

        }
    }


    protected virtual void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (TryGetComponent(out Collider2D collider2D))
        {
            collider2D.enabled = false;
        }
        if (animator != null)
        {
            animator.SetInteger("state", (int)EnumState.Die);
        }
    }


    public override float Damage(GameObject source, float value)
    {
        float ret = -1;
        if (running)
        {
            Constant.Sounds.EnemyOnHit.PlayOneShot();
            ret = value;
            health -= value;
            if (health <= 0)
            {
                ret = health + value;
                Die();
            }
        }
        return ret;
    }


    public override void Die()
    {
        Die(state, out state);
    }

    protected virtual void Die(EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (running)
        {
            outState = EnumState.Die;
            if(dieAction.done){
                dieAction.ReStart();
            }
        }
    }


    public virtual Vector2 GetBodyPartPosition(Person.BodyPart bodyPart)
    {
        Vector2 ret;
        switch (bodyPart)
        {
            case Person.BodyPart.Body:
                {
                    ret = transform.position;
                }
                break;
            default:
                throw new System.NotImplementedException();
        }
        return ret;
    }

    public override bool IsDie()
    {
        return state == EnumState.Die;
    }

    protected GameObject CreateBoom(Vector2 position){
        GameObject ret=Instantiate(boomPrefab,position,Quaternion.identity);
        return ret;
    }
}
